package com.longjianghuasheng.logistics.business.mine.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseTitleFragment
import com.longjianghuasheng.logistics.business.mine.viewmodel.FeedbackViewModel
import com.longjianghuasheng.logistics.databinding.FragmentFeedBackBinding
import com.longjianghuasheng.logistics.ext.observer
import com.longjianghuasheng.logistics.ext.selectPictureMore
import com.longjianghuasheng.logistics.weight.GlideEngine
import com.longjianghuasheng.logistics.weight.recyclerview.ImageSelectGridAdapter
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.entity.LocalMedia

class FeedbackFragment : BaseTitleFragment<FeedbackViewModel, FragmentFeedBackBinding>(),
    ImageSelectGridAdapter.OnAddPicClickListener {


    private val mAdapter by lazy {
        ImageSelectGridAdapter(requireContext(), this)
    }

    private val mSelectList by lazy {

        mutableListOf<LocalMedia>()
    }

    @SuppressLint("NotifyDataSetChanged")
    private val pictureResult: (List<LocalMedia>) -> Unit = {


        mSelectList.clear()
        mSelectList.addAll(it.toMutableList())


        LogUtils.d("mSelectList=$mSelectList")
        mAdapter.setSelectList(mSelectList)
        mAdapter.notifyDataSetChanged()

    }

    override fun layoutId(): Int = R.layout.fragment_feed_back

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        initTitleBar("意见反馈")

        mDatabind.run {


            radioGroup.setOnCheckedChangeListener { group, checkedId ->

                mViewModel.type = when (checkedId) {
                    R.id.rb_function -> 1
                    R.id.rb_rule -> 2
                    R.id.rb_other -> 3
                    else -> 0
                }


            }

            btnCommit.setOnClickListener {

                mViewModel.run {
                    content = editText.contentText
                    realPathList.clear()
                    realPathList.addAll(mSelectList.map { it.realPath })
                    feedback()

                }
            }



            mAdapter.setSelectList(mSelectList)

            mAdapter.setSelectMax(3)
            mAdapter.setOnItemClickListener { position, v ->
                PictureSelector.create(this@FeedbackFragment)
                    .themeStyle(R.style.picture_default_style)
                    .isNotPreviewDownload(true)//是否显示保存弹框
                    .imageEngine(GlideEngine.createGlideEngine()) // 选择器展示不出图片则添加
                    .openExternalPreview(position, mSelectList)

            }
            rvUpload.run {

                layoutManager = GridLayoutManager(
                    activity, 3, RecyclerView.VERTICAL, false
                )

                adapter = mAdapter
                setHasFixedSize(true)
                isNestedScrollingEnabled = false
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {
            navigateUp()
        }
    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
    }

    override fun onAddPicClick() {
        selectPictureMore(mSelectList, pictureResult)

    }
}